<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="clickAddRole"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="rolelist">
              <el-table-column type="index" label="序号" width="120" />
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template #default="{ row }">
                  <el-button size="small" type="success" @click="setPermission(row.id)">分配权限</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click="clickEditRole(row.id)"
                  >编辑</el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="delRole(row.id)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                layout="prev,pager,next"
                :total="page.total"
                :page-size="page.pagesize"
                :current-page="page.page"
                @current-change="currentChange"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="公司名称">
                <el-input
                  v-model="companyInfo.name"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  v-model="companyInfo.companyAddress"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  v-model="companyInfo.mailbox"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="companyInfo.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 弹出层 -->
      <el-dialog title="编辑弹层" :visible="showDialog" @close="btnCancel">
        <el-form
          v-if="showDialog"
          ref="roleForm"
          :model="roleForm"
          :rules="rules"
          label-width="120px"
        >
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="btnCancel">取消</el-button>
            <el-button
              size="small"
              type="primary"
              @click="btnOK"
            >确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>

      <!-- 分配权限弹出层 -->
      <el-dialog title="分配权限" :visible="showPermDialog" @close="btnPermCancel">
        <el-tree
          ref="permTree"
          :data="treeData"
          :props="{label:'name'}"
          default-expand-all
          show-checkbox
          check-strictly
          node-key="id"
        />
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button type="primary" size="small" @click="btnPermOK">确定</el-button>
            <el-button size="small" @click="btnPermCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getCompanyById, getRoleList, delRoleById, addRole, getRoleById, editRoleById, assignPrem } from '@/api/setting'
import { getPermissionList } from '@/api/permission'
import { listToTreeData } from '@/utils'
export default {
  data() {
    return {
      // 公司基本信息
      companyInfo: {},
      // 角色列表
      rolelist: [],
      // 角色列表请求参数
      page: {
        page: 1,
        pagesize: 4,
        // 角色列表参数总条数
        total: 0
      },
      // 模态框是否显示
      showDialog: false,
      // 角色信息
      roleForm: {
        // 名字
        name: '',
        // 角色描述
        description: ''
      },
      rules: {
        name: [{ required: true, message: '此项必填', trggier: 'blur' }]
      },
      // 分配权限
      showPermDialog: false,
      // 权限树形数据
      treeData: [],
      // 角色id
      roleId: ''
    }
  },
  created() {
    // 获取公司基本信息
    this.getCompanyById()
    // 获取所有角色列表
    this.getRoleList()
    this.getPermissionList()
  },
  methods: {
    async btnPermOK() {
      const checkedKeys = this.$refs.permTree.getCheckedKeys()
      console.log(checkedKeys)
      await assignPrem({
        id: this.roleId,
        permIds: checkedKeys
      })
      this.showPermDialog = false
      this.$message.success('操作成功')
    },
    btnPermCancel() {
      // 清数据
      this.$refs.permTree.setCheckedKeys([])
      // 关闭弹窗
      this.showPermDialog = false
    },
    // 权限列表
    async getPermissionList() {
      this.treeData = listToTreeData(await getPermissionList(), '0')
      // console.log(this.treeData)
    },
    // 点击分配权限
    async setPermission(id) {
      this.roleId = id
      const { permIds } = await getRoleById(id)
      console.log(permIds)
      this.showPermDialog = true
      this.$nextTick(() => {
        this.$refs.permTree.setCheckedKeys(permIds)
      })
    },
    // 获取公司基本信息
    async getCompanyById() {
      const res = await getCompanyById(this.$store.state.user.userInfo.companyId)
      // console.log(res)
      this.companyInfo = res
    },
    // 获取所有角色列表
    async getRoleList() {
      const { rows, total } = await getRoleList(this.page)
      this.rolelist = rows
      this.page.total = total
    },
    // 点击分页改变
    currentChange(val) {
      this.page.page = val
      this.getRoleList()
    },
    // 删除
    async delRole(id) {
      // 询问
      await this.$confirm('确定要删除嘛？')
      // 发请求
      await delRoleById(id)
      // 提示
      this.$message.success('操作成功')
      if (this.rolelist.length === 1 && this.page.page > 1) {
        this.page.page--
      }
      // 刷新数据
      this.getRoleList()
    },
    //  关闭模态框的操作
    async btnCancel() {
      // 清空校验
      await this.$refs.roleForm.resetFields()
      // 清除数据
      this.roleForm = {
        name: '',
        description: ''
      }
      // 关闭窗口
      this.showDialog = false
    },
    // 模态框点击确认
    async btnOK() {
      // 校验
      await this.$refs.roleForm.validate()
      if (this.roleForm.id) {
        // 编辑
        await editRoleById(this.roleForm)
      } else {
        // 新增
        await addRole(this.roleForm)
        // 跳到最后一页
        this.page.page = Math.ceil((this.page.total + 1) / this.page.pagesize)
      }
      // 提示
      this.$message.success('操作成功')
      // 刷新数据
      this.getRoleList()
      // 关闭窗口
      this.btnCancel()
    },
    // 点击新增角色
    clickAddRole() {
      // 打开模态框
      this.showDialog = true
    },
    // 点击编辑
    async clickEditRole(id) {
      // 回显数据
      this.roleForm = await getRoleById(id)
      // 打开模态框
      this.showDialog = true
    }
  }
}
</script>

<style>
</style>
